package graphics.object.testing;

import static org.lwjgl.opengl.GL11.GL_BLEND;
import static org.lwjgl.opengl.GL11.GL_QUADS;
import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D;
import static org.lwjgl.opengl.GL11.glBegin;
import static org.lwjgl.opengl.GL11.glColor3f;
import static org.lwjgl.opengl.GL11.glDisable;
import static org.lwjgl.opengl.GL11.glEnable;
import static org.lwjgl.opengl.GL11.glEnd;
import static org.lwjgl.opengl.GL11.glVertex2i;

import graphics.object.GLRectangle;
import graphics.object.interfaces.GLMouseListener;

import java.awt.Color;

public class GLTestingRectangle extends GLRectangle implements GLMouseListener{

    private static final long serialVersionUID = 1L;

    public Color rectcolor;
    public Color hoverColor = new Color(200, 200, 200);
    public Color renderColor;

    private boolean isSelected = false;

    public GLTestingRectangle(Color rectcolor, int x, int y, int width, int height){
        super(x, y, width, height);
        this.rectcolor = rectcolor;
        renderColor = rectcolor;
    }

    public void onRender(){
        glDisable(GL_BLEND);
        glDisable(GL_TEXTURE_2D);

        glBegin(GL_QUADS);
        glColor3f(renderColor.getRed() / 255f, renderColor.getGreen() / 255f, renderColor.getBlue() / 255f);
        glVertex2i(this.x, this.y);
        glVertex2i(this.x + width, this.y);
        glVertex2i(this.x + width, this.y + this.height);
        glVertex2i(this.x, this.y + this.height);
        glEnd();

        glEnable(GL_TEXTURE_2D);
        glEnable(GL_BLEND);
    }

    public void onMouseMove(int x, int y, int dx, int dy){
        if(this.contains(x, y)){
            renderColor = hoverColor;
        }else{
            renderColor = rectcolor;
        }
        
        if(isSelected){
            this.x += dx;
            this.y -= dy;
        }
    }

    public void onMousePress(int id, int x, int y) {
        if(id == 0 && contains(x, y)){
            rectcolor = new Color((float)Math.random(), (float)Math.random(), (float)Math.random());
            renderColor = rectcolor;
            
            isSelected = true;
        }
    }

    public void onMouseRelease(int id, int x, int y) {
        if(isSelected && id == 0){
            isSelected = false;
        }
    }
}
